new Vue({
    el: '#app',
    data: {
        // 加载
        loading: false,
        loadingEditNote: false,
        // 表格中的笔记
        tableNotes: [{
            id: 1,
            content: '笔记内容',
            time: '2024-09-09',
            open: 1,
        }],
        // 选中的笔记
        selectedNotes: [],
        // 查询条件
        queryCondition: {
            content: "",
            open: [],
        },
        openOptions: ['不公开', "公开"],
        // 正在修改笔记
        updatingNote: false,
        updateNote: {},
        curPrefixUrl: prefixUrl + gatewayPrefix.note
    },
    methods: {
        goBack() {
            goMain();
        },
        // 查看笔记
        seeNoteDetail(row) {
            showAlert(this, row);
        },
        cancelEditNote() {
            this.updatingNote = false;
        },
        finishEditNote() {
            const noteUpdateDTO = {
                id: this.updateNote.id,
                content: this.updateNote.content,
                open: this.updateNote.open,
            };
            // showAlert(this, noteUpdateDTO);
            const url = this.curPrefixUrl + "/user/notes";
            this.loadingEditNote = true;
            axios.put(url, noteUpdateDTO).then((resp) => {
                const result = resp.data;
                if (result.code === resultConstant.success) {
                    showMsg(this, '修改成功');
                    this.query();
                } else {
                    showMsg(result.msg);
                }
            }).catch((error) => {
                showMsg(error);
            }).finally(() => {
                this.updatingNote = false;
                this.loadingEditNote = false;
            });
        },
        // 点击了编辑笔记
        editNote(row) {
            // showAlert(this, row);
            this.updateNote = {
                id: row.id,
                content: row.content,
                open: row.open === '公开' ? openStatus.open + "" : openStatus.notOpen + ""
            };
            this.updatingNote = true;
        },
        // 删除笔记
        deleteNote(row) {
            showConfirmAlert(this, '确定删除当前笔记吗？',
                () => {
                    // showAlert(this, row);
                    this.deleteThisNote(row);
                },
                () => {
                    showMsg(this, '已取消删除');
                })
        },
        // 删除笔记的具体实现
        deleteThisNote(row) {
            const url = this.curPrefixUrl + "/user/notes/" + row.id;
            axios.delete(url).then((resp) => {
                const result = resp.data;
                if (result.code === resultConstant.success) {
                    showMsg(this, '删除成功');
                    this.query();
                } else {
                    showMsg(result.msg);
                }
            }).catch((error) => {
                showMsg(error);
            }).finally(() => {
            });
        },
        handleSelectionChange(val) {
            this.selectedNotes = val;
            console.log("this.selectedQuestion=" + JSON.stringify(this.selectedNotes))
        },
        // 查询笔记
        getNotes(noteSelfQueryDTO) {
            this.tableNotes = [];
            this.loading = true;
            const url = this.curPrefixUrl + "/user/notes/condition/my";
            console.log("noteSelfQueryDTO=" + JSON.stringify(noteSelfQueryDTO));
            axios.get(url, {
                params: noteSelfQueryDTO
            }).then((resp) => {
                const result = resp.data;
                this.addNotesToTable(result.data);
            }).finally(() => {
                this.loading = false;
            });
        },
        addNotesToTable(notes) {
            for (let i = 0; i < notes.length; i++) {
                this.tableNotes.push({
                    id: notes[i].id,
                    content: notes[i].content,
                    time: notes[i].time,
                    open: notes[i].open === 1 ? '公开' : '不公开'
                });
            }
        },
        // 点击了筛选
        query() {
            let noteSelfQueryDTO;
            // 选了2个和不选都是查询所有公开与否
            if (this.queryCondition.open.length === 2 || this.queryCondition.open.length === 0) {
                noteSelfQueryDTO = {
                    content: this.queryCondition.content
                }
            } else if (this.queryCondition.open.length === 1) {
                // 选了一个
                noteSelfQueryDTO = {
                    content: this.queryCondition.content,
                    open: this.queryCondition.open[0]
                }
            }
            this.getNotes(noteSelfQueryDTO);
        },
        deleteSome() {
            showConfirmAlert(this, '确定删除当前选择的题目吗？',
                () => {
                    this.deleteSelectedNotes();
                },
                () => {
                    showMsg(this, '已取消删除');
                })
        },
        deleteSelectedNotes(){
            const url = this.curPrefixUrl + "/user/notes/delete/many";
            let ids = [];
            for (let i = 0; i < this.selectedNotes.length; i++) {
                ids.push(this.selectedNotes[i].id);
            }
            const data = {
                ids: ids
            };
            // showAlert(this, data);
            this.loading = true;
            axios.post(url, data).then((resp) => {
                const result = resp.data;
                if (result.code === resultConstant.success) {
                    showMsg(this, '删除成功');
                    // 获取题目
                    this.query()
                } else {
                    showMsg(result.msg);
                }
            }).catch((error) => {
                showAlert(this, error);
            }).finally(() => {
                this.loading = false;
            });
        }
    },
    created() {
        saveSessionStorage(this, storageKey.noteManageData, () => {
            // 获取笔记列表
            this.getNotes();
        })
    }
});